home *** CD-ROM | disk | FTP | other *** search
- //
- // todo2rtf.cpp : Convert from todo database to RTF.
- //
-
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <time.h>
-
- // Struct...
- class Todo {
- public:
- char *_str;
- long int _date;
- long int _completed;
- long int _priority;
- long int _category;
- };
-
-
- long int
- GetLong(FILE *fp)
- {
- // This will work on big-endian or small-endian...even if its
- // a little slower...
- unsigned long int ret;
- unsigned char buf[4];
-
- buf[0] = getc(fp);
- buf[1] = getc(fp);
- buf[2] = getc(fp);
- buf[3] = getc(fp);
-
- ret = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
- return ret;
- }
-
-
- short int
- GetWord(FILE *fp)
- {
- // This will work on big-endian or small-endian...even if its
- // a little slower...
- short int ret;
- char buf[2];
-
- buf[0] = getc(fp);
- buf[1] = getc(fp);
-
- ret = (buf[1] << 8) | buf[0];
- return ret;
- }
-
-
- //
- // NOTE: This returns a static pointer to a buffer, copy the string
- // or lose it...
- //
- char *
- GetString(FILE *fp)
- {
- static char buf[20000]; // LOnger than longest!!!
-
- unsigned int len = getc(fp);
-
- if (len == 0)
- buf[0] = '\0';
-
-
- if (len == 0xff) {
- len = getc(fp);
- len = len | (getc(fp) << 8);
- }
-
- char *bp = buf;
- for (int i=0; i < len; i++)
- *bp++ = getc(fp);
-
- *bp = '\0';
- return buf;
- }
-
- // I think that the max is 12...
- char *categories[32];
-
-
- // This is used to hold the order for categories when using the -r
- // option which arranges categories in the order I want.
- int rickmap[32];
- int rickorder = 0;
-
-
- int
- main(int argc, char* argv[])
- {
- int i;
-
- // Initialize the categories and rickmap to start with 0's
- for (i=0; i < 32; i++) {
- categories[i] = 0;
- rickmap[i] = 0;
- }
-
- // Category 0 is the unfiled category by definition
- categories[0] = "Unfiled";
- rickorder = 0;
-
- // First argument MUST be a file name of a todo file
- if (argc < 2) {
- printf("File to open required...cannot continue\n");
- exit(2334);
- }
-
- // Open the todo.dat file
- FILE *fp = fopen(argv[1], "rb");
- if (!fp) {
- printf("Could not open file %s...cannot continue\n", argv[1]);
- exit(2334);
- }
-
- // Collect what flags are set
- int printUnfiled = 0;
- int min = 1, max = 5;
- int textMode = 0;
- char *fname = "todo.rtf";
- for (i = 2; i < argc; i++) {
- if (strcmp(argv[i], "-u") == 0) {
- // Print unfiled category items
- printUnfiled = 1;
- } else if (strcmp(argv[i], "-r") == 0) {
- // Use the rick order of categories
- rickorder = 1;
- } else if (strcmp(argv[i], "-t") == 0) {
- // Print in text mode instead of RTF
- textMode = 1;
- } else if (strcmp(argv[i], "-h") == 0) {
- // Set the maximum priority todo to print
- max = atoi(argv[i+1]);
- i++;
- } else if (strcmp(argv[i], "-l") == 0) {
- // Set the minimum priority todo to print
- min = atoi(argv[i+1]);
- i++;
- } else if (strcmp(argv[i], "-p") == 0) {
- // Set the min/max priority todo to print the same
- min = max = atoi(argv[i+1]);
- i++;
- } else if (strcmp(argv[i], "-o") == 0) {
- // Use a different output file
- fname = argv[i+1];
- i++;
- } else {
- // I dont know this flag
- printf("Unknown flag %s ignored\n", argv[i]);
- }
- }
-
- // Read through input section
- GetLong(fp);
- GetString(fp);
- GetString(fp);
- GetLong(fp);
-
- // Get the number of categories
- int numCats = GetLong(fp);
-
- // Where to start putting categories based on the
- // rickorder. If we are doing rickorder then start at 4 because I
- // have 4 pre-sorted categories (ie: I want these guys first).
- // If you change the categories make sure and update this number
- int indexStart = 0;
- if (rickorder)
- indexStart = 4;
-
- // Go through the categories and put them in the map which keeps track
- // of their order
- for (i = 0; i < numCats; i++) {
- int index = GetLong(fp);
- GetLong(fp);
- GetLong(fp);
- char *cat = GetString(fp); // Keep the long name
-
- // printf("Category: index: %d str: %s\n", index, cat);
-
- categories[index] = new char [strlen(cat) + 1];
- strcpy(categories[index], cat);
-
- // If -r is set then I want the first four categories to be
- // Today, work, home, and programs. After that I don't care
- // what order they come in
- if (rickorder) {
- if (strcmp(cat, "Today") == 0)
- rickmap[0] = index;
- else if (strcmp(cat, "Work") == 0)
- rickmap[1] = index;
- else if (strcmp(cat, "Home") == 0)
- rickmap[2] = index;
- else if (strcmp(cat, "Programs") == 0)
- rickmap[3] = index;
- else
- rickmap[indexStart++] = index;
- } else {
- // If not in rick order this will just fill up by index
- rickmap[indexStart++] = index;
- }
- GetString(fp); // Burn the short name
- }
-
- // printf("cat order\n");
- // for (i = 0; i < numCats; i++) {
- // printf("%d == %s\n", rickmap[i], categories[rickmap[i]]);
- // }
-
- // Now look at the table-description (whatever that is...)
- GetLong(fp);
- int numFields = GetLong(fp);
- GetLong(fp);
- GetLong(fp);
- GetLong(fp);
- GetWord(fp);
-
- for (i = 0; i < numFields; i++) {
- GetWord(fp);
- }
-
- int numTodos = GetLong(fp);
-
- // For some martian reason, this is *10...
- numTodos = numTodos / 10;
-
- // printf("Number of ToDo's: %d\n", numTodos);
-
- // Allocate data structures...and a spare for fun
- Todo *todos = new Todo[numTodos + 1];
-
- for (i=0; i < numTodos; i++) {
-
- GetLong(fp);
- GetLong(fp);
-
- GetLong(fp);
- GetLong(fp);
-
- GetLong(fp);
- GetLong(fp);
-
- GetLong(fp);
- GetLong(fp);
-
- char *s = GetString(fp);
- todos[i]._str = new char [strlen(s) + 1];
- strcpy(todos[i]._str, s);
-
- GetLong(fp); // 3 = date
- todos[i]._date = GetLong(fp);
-
- GetLong(fp); // 6 = Bool
- todos[i]._completed = GetLong(fp);
-
- GetLong(fp); // 1 = int
- todos[i]._priority = GetLong(fp);
-
- GetLong(fp);
- GetLong(fp);
-
- GetLong(fp); // 1 = int
- todos[i]._category = GetLong(fp);
-
- GetLong(fp); // 5 = Cstr
- GetLong(fp); // just a 0
-
- GetString(fp); // Eat the note!
- }
-
-
- // Get output file ready to go
- FILE *fout = fopen(fname, "w");
- if (!fout) {
- printf("Could not open file %s...cannot continue\n", fname);
- exit(2335);
- }
-
- if (!textMode) {
- fprintf(fout, "{\\rtf1\\ansi\\deff0\\deftab720{\\fonttbl{\\f0\\fswiss\\fprq2 Arial;}}");
- fprintf(fout, "\\deflang1033\\pard\\plain\\f0\\fs20");
- }
-
- // Print by category, and by priority
- // Go through the different categories
- int p;
- char dbuf[256];
- char *dptr;
- for (i = 0; i <= numCats; i++) {
-
- int c = rickmap[i];
-
- // Skip if this is the unfiled category and don't want to print it
- if (!printUnfiled && (c == 0))
- continue;
-
- int anyInCat = 0;
-
- // Print stuff in priority order within categories
- for (p = min; p <= max; p++) {
-
- for (int j = 0; j < numTodos; j++) {
-
- // If priority matches what we are looking at and in the right category then
- // go to town;
- if ((todos[j]._priority == p) && (todos[j]._category == c)) {
-
- // IF this is the first printing for this category then print
- // a header
- if (!anyInCat) {
- if (!textMode)
- fprintf(fout, "\\par\\par\\plain\\f0\\b\\ul\\fs24 %s\n",
- categories[c]); // bold big
- else {
- fprintf(fout, "\n%s\n", categories[c]);
- fprintf(fout, "--------------------\n", categories[c]);
- }
-
- anyInCat = 1;
- }
-
- // If the item is not completed prefix with an o, else an x
- char comp = 'o';
- if (todos[j]._completed)
- comp = 'x';
-
- time_t bob = todos[j]._date;
- struct tm *nt;
- nt = localtime(&bob);
- // NOTE: Check out the +1 for month, +1900 for year!!!
- sprintf(dbuf, " - (%d/%d/%d)", nt->tm_mon+1, nt->tm_mday, nt->tm_year+1900);
-
- // No date if past date 2030:
- if (nt->tm_year > 130)
- dptr = " ";
- else
- dptr = dbuf;
-
- if (!textMode) {
- fprintf(fout, "\\par\\plain\\f0\\fs20\\b %c \\b0 %d: %s %s\n",
- comp, p, todos[j]._str, dptr);
- } else {
- fprintf(fout, "%c %d: %s %s\n", comp, p, todos[j]._str, dptr);
- }
- }
- }
- }
- }
-
- if (!textMode)
- fprintf(fout, "\\par }\n");
-
- fclose(fp);
- fclose(fout);
- return 0;
- }
-
-